package zero.tech.def.xe;

import java.util.List;
import java.util.Set;
import java.util.TreeMap;

import org.apache.commons.lang3.StringUtils;

import zero.tech.core.util.EngineLogUtil;
import zero.tech.def.DefClassLoader;
import zero.tech.def.Describle;

public class XETool {
	public static String downpath = System.getProperty("user.dir")
			+ "/def/excel/" == null ? "F:/workspace/maveneclipse/cs_resource/src/main/webapp/excel/"
			: System.getProperty("user.dir") + "/def/excel/";
	public static void start(boolean createXml,String downpathtmp) throws Exception {
		if(StringUtils.isNotBlank(downpathtmp)) {
			downpath=downpathtmp;
		}
		Set<Class<?>> classSet = DefClassLoader.getManager().getClasses();
		TreeMap<String, List<MyField>> treeMapXML = new TreeMap<String, List<MyField>>();
		TreeMap<String, List<MyField>> treeMapExcel = new TreeMap<String, List<MyField>>();
		for (Class<?> claz : classSet) {
			// if (BaseDef.class.isAssignableFrom(claz)) {
			if (claz.getAnnotation(Describle.class) == null) {
				continue;
			}
			// xml信息
			try {
				List<MyField> filedsXml = XmlTool.fieldOne(claz);
				treeMapXML.put(claz.getName(), filedsXml);
				// excel信息
				List<MyField> filedsExcel = XmlTool.getFieldDesc(claz);
				String desc_clazz = DefDataTool.getClassDescrible(claz);
				treeMapExcel.put(desc_clazz + "=" + claz.getName(), filedsExcel);
			} catch (Exception e) {
				EngineLogUtil.traceFull(e);
			}
			// }
		}
		if (createXml) {
			XmlTool.convertToXml(treeMapXML, createXml);
		}
		ExcelTool.converToExcel(treeMapExcel,downpath);
	}

}
